import cv2
import numba
from numba import jit
import math
import numpy as np


@jit(nopython=True)
def urlWriter(username, password, ip, channel):
    url: str = ("rtsp://%s:%s@%s//Streaming/Channels/%d" % (username, password, ip, channel))
    return url

@jit(nopython=True)
def cylindrical_projection(img, f):
    rows = img.shape[0]
    cols = img.shape[1]

    blank = np.zeros_like(img)
    center_x = int(cols / 2)
    center_y = int(rows / 2)

    for y in range(rows):
        for x in range(cols):
            theta = math.atan((x - center_x) / f)
            point_x = int(f * math.tan((x - center_x) / f) + center_x)
            point_y = int((y - center_y) / math.cos(theta) + center_y)

            if point_x >= cols or point_x < 0 or point_y >= rows or point_y < 0:
                pass
            else:
                blank[y, x, :] = img[point_y, point_x, :]
    return blank


if __name__ == '__main__':
    cap = cv2.VideoCapture(0)
    if cap.isOpened():
        print("连接成功")
    else:
        print("连接失败")
    fps = cap.get(cv2.CAP_PROP_FPS)
    print(fps)
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    print(size)

    while (True):
        # Capture frame-by-frame
        ret, frame = cap.read()
        # Our operations on the frame come here

        img = cylindrical_projection(frame, 500)

        # Display the resulting frame
        cv2.imshow('frame', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
